package hr_project.hr.web;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import hr_project.hr.biz.Emp.IEmpBiz;
import hr_project.hr.biz.Salary.ICheckResultBiz;
import hr_project.hr.biz.Salary.ISalaryBiz;
import hr_project.hr.biz.Salary.ISalaryDatailBiz;
import hr_project.hr.entity.Check_Result;
import hr_project.hr.entity.Department;
import hr_project.hr.entity.Emp;
import hr_project.hr.entity.Salary;
import hr_project.hr.entity.Salary_standerd_info;
import hr_project.hr.util.QuerySalary;
import hr_project.hr.util.pagebean;

/**
 * @author 冯亚倩
 * */

@Controller
@RequestMapping("/salary")
public class SalaryAction {
	
	@Autowired
	private ISalaryBiz biz;
	
	@Autowired
	private IEmpBiz ebiz;
	
	@Autowired
	private ISalaryDatailBiz dbiz;
	 
	@Autowired
	private ICheckResultBiz cbiz;

	public ISalaryBiz getBiz() {
		return biz;
	}

	public void setBiz(ISalaryBiz biz) {
		this.biz = biz;
	}
	
    public IEmpBiz getEbiz() {
		return ebiz;
	}

	public void setEbiz(IEmpBiz ebiz) {
		this.ebiz = ebiz;
	}

	public ISalaryDatailBiz getDbiz() {
		return dbiz;
	}

	public void setDbiz(ISalaryDatailBiz dbiz) {
		this.dbiz = dbiz;
	}

	public ICheckResultBiz getCbiz() {
		return cbiz;
	}

	public void setCbiz(ICheckResultBiz cbiz) {
		this.cbiz = cbiz;
	}

	//准备增加薪资标准
	@RequestMapping("/addsal")
	public String addSal(HttpSession sess,long said,long id)
	{
		Salary ss=biz.getById(said);
		Emp e=ebiz.getById(id);
		ss.setEmpxx(e);
		ss.setSa_status("新增");
		sess.setAttribute("sal", ss);
	    return "salary_add";
	}
	
	//增加薪资明细
	@RequestMapping("/detail_sal")
	public ModelAndView addDetail(Salary_standerd_info sdet,@RequestParam(defaultValue="1")int n,HttpSession sess)
	{
		ModelAndView mv=new ModelAndView();
		//取
		List<Salary_standerd_info> list=(List<Salary_standerd_info>)sess.getAttribute("salist");
		if(list==null) list=new ArrayList<Salary_standerd_info>();
		
		//改
		
		list.add(sdet);
		System.out.println(list);
		
		//存
		Salary sa=(Salary)sess.getAttribute("sal");
		//总金额
        if(sdet.getItem().equals("基本工资"))
        {
        	sa.setSa_base(sdet.getMoney());
        }
        Salary s1=(Salary)sess.getAttribute("sal");
		Double money=s1.getSa_totalmoney()+sdet.getMoney();
		s1.setSa_totalmoney(money);
		sess.setAttribute("sal", s1);
		
		sa.setList(list);
		sess.setAttribute("sdet", sa);
		
		sess.setAttribute("salist", list);

		mv.setViewName("salary_add");

		return mv;
	}
	
	
	  
	//删除薪资明细
	@RequestMapping("/del")
	public 	ModelAndView delSal_detail(int index,HttpSession sess)
	{
		ModelAndView mv=new ModelAndView();
		//取
		List<Salary_standerd_info> list=(List<Salary_standerd_info>)sess.getAttribute("salist");
		
		//改
		Salary_standerd_info si=list.get(index);

		//存
		list.remove(index);
	
		
		//总金额
		Salary s1=(Salary)sess.getAttribute("sal");
		Double money=s1.getSa_totalmoney()-si.getMoney();
		if(list.size()==0)
		{
			s1.setSa_totalmoney(s1.getSa_base());
		}
		s1.setSa_totalmoney(money);
		sess.setAttribute("sal", s1);
		
		sess.setAttribute("salist", list);
		
		
	    mv.setViewName("salary_add");
		
		return mv;
	}
	
	
	//确认添加薪资
	@RequestMapping("/addok")
	public ModelAndView addok(Long sa_id,@RequestParam(defaultValue="1")int n,HttpSession sess)
	{
		ModelAndView mv=new ModelAndView();
		
		Salary ss=(Salary)sess.getAttribute("sal");
		List<Salary_standerd_info> list=(List<Salary_standerd_info>)sess.getAttribute("salist");
		ss.setList(list);
		ss.setSa_status("新增");
		biz.modSalary(ss, list);
		sess.removeAttribute("sal");
		sess.removeAttribute("salist");		
		if(n==2)
		{
			mv.setViewName("salary_mod");
		}
		else
		{
			mv.setViewName("salary_list");
		}
		return mv;
	}
	
	
	//取消添加薪资标准
	@RequestMapping("/cancle")
	public String cancle(Long sa_id,int index,HttpSession sess)
	{
		sess.removeAttribute("sal");
		sess.removeAttribute("salist");	
	
		return "sal_list";
	}
	
	
	//根据id，查看信息      进去审核页面  。。。
	@RequestMapping("/getbyid")
	public ModelAndView getById(ModelAndView mv,long sa_id)
	{
		System.out.println("************"+sa_id);
		mv.addObject("sa", biz.getById(sa_id));
		mv.setViewName("salary_mod");
		return mv;
	}
	
	//删除薪资明细
	@RequestMapping("/delxx")
	public String del_sal(long si_id,long sa_id,HttpSession sess)
	{
		System.out.println("薪资明细编号：***"+si_id);
		System.out.println("薪资编号：***"+sa_id);
		
		Salary ss=biz.getById(sa_id);
		System.out.println("标准*****"+ss.getSa_id());
		
		System.out.println("++++++++++++下边是明细+++++++++++++");
		Salary_standerd_info si=dbiz.getById_det(si_id);
		/*System.out.println("明细：-----------"+si.getSi_id());*/
		System.out.println("明细id：+++++++++++"+si_id);
		System.out.println("金额：********"+si.getMoney());
		dbiz.delDetail_sal(si_id);
		double money=ss.getSa_totalmoney()-si.getMoney();
		
		System.out.println("薪资总金额：---"+money);
		
		ss.setSa_totalmoney(money);
		
        biz.modSalary(ss, null);
		return "redirect:/salary/getbyid.action?sa_id="+sa_id;
	}
	
	//更新薪资标准
	@RequestMapping("/update")
	public String Update(Salary sal)
	{
		biz.update_sal(sal);
		return "salary_mod";
	}
	
	
	//修改工资标准的的状态  -- 已删除
	@RequestMapping("/modstu")
	public String modstatus(long sa_id,HttpSession sess)
	{
		Salary ss=biz.getById(sa_id);
		ss.setSa_status("已删除");

		biz.update_sal(ss);
		return "salary_list";
	}
	
	//提交审核(提交薪资审核)
	@RequestMapping("/up_check")
	public String up_check(long sa_id,long did,HttpSession sess)
	{
		Salary ss=biz.getById(sa_id);
		ss.setSa_status("待审核");

		Emp man=ebiz.getManageById(did);
		ss.setUs_checkid(man.getUs_id());
		biz.update_sal(ss);
		return "salary_list";
		
	}
	
	//根据id进入审核页面
	@RequestMapping("/apply")
	public ModelAndView get_apply(long sa_id,@RequestParam(defaultValue="1")int n,ModelAndView mv)
	{
		mv.addObject("sa", biz.getById(sa_id));
		if(n==1)
		{
			mv.setViewName("salary_apply1");
		}
		else
		{
			mv.setViewName("salary_apply2");
		}
		
		return mv;
	}
	
	
	//部门经理审核
	@RequestMapping("/apply1")
	public String apply_one(Check_Result cr,HttpSession sess)
	{
		//添加处理人
		Emp e=(Emp)sess.getAttribute("loger");
		System.out.println("部门经理*********"+e.getUs_id());
		cr.setCheck_uid(e.getUs_id());
		System.out.println("审核结果id*********"+cr.getRe_id());
		cbiz.addResult(cr);
		
		//修改薪资的状态
		Salary sx=new Salary();
		sx.setSa_id(cr.getSa_id()); //获取薪资的id
		Salary ss=biz.getById(cr.getSa_id()); //根据薪资id，查找薪资信息
		//在进行薪资处理之前进行判断
		//在通过的情况下，进行薪资的审核
		 //1.如果总额小于 10000  会计处理 //2.如果总额大于10000 总经理处理
		if(cr.getResults().equals("通过"))
		{
			//1.总金额小于10000
			if(ss.getSa_totalmoney()<10000)
			{
				Emp e1=new Emp();
				e1.setUs_id(6l);
				ss.setUs_checkid(e1.getUs_id());
				ss.setSa_status("待发放");
			}
			else //1.总金额大于10000
			{
				Emp e2=new Emp();
				e2.setUs_id(1l);
				ss.setUs_checkid(e2.getUs_id());
				ss.setSa_status("复审");
			}
			
		}
		else //若是通过之外的，则修改它的状态
		{
			ss.setSa_status(cr.getResults());
		}
		biz.update_sal(ss);
		return "sal_check_list1";
	}
	
	//总经理(复审)
	@RequestMapping("/apply2")
	public String apply_two(Check_Result cr,HttpSession sess)
	{
		//添加处理人
		Emp e=(Emp)sess.getAttribute("loger");
		System.out.println("部门经理*********"+e.getUs_id());
		cr.setCheck_uid(e.getUs_id());
		System.out.println("审核结果id*********"+cr.getRe_id());
		cbiz.addResult(cr);
		
		//修改薪资的状态
		Salary sx=new Salary();
		sx.setSa_id(cr.getSa_id()); //获取薪资的id
		Salary ss=biz.getById(cr.getSa_id()); //根据薪资id，查找薪资信息

		if(cr.getResults().equals("通过"))
		{
			
				Emp e1=new Emp();
				e1.setUs_id(1l);
				System.out.println(e1.getUs_name());
				ss.setUs_checkid(e1.getUs_id());
				ss.setSa_status("待发放");
			
		}
		else //若是通过之外的，则修改它的状态
		{
			ss.setSa_status(cr.getResults());
			System.out.println(ss.getSa_status());
		}
		biz.update_sal(ss);
		return "sal_check_list2";
	}
	
	
	
	//部门经理：列表页面(ajax实现)
	@RequestMapping("/check_list")
	@ResponseBody
	public pagebean getPage1(@RequestParam(defaultValue="1")int page,QuerySalary qs,HttpSession sess)
	{
		Emp e=(Emp)sess.getAttribute("loger");
		System.out.println(e.getUs_id()+","+e.getUs_name());
		pagebean pb=biz.getPage1(qs, page, e.getUs_id());
	    
		sess.setAttribute("pb", pb);
		return pb;
	}
	
	
	//部门经理：列表页面(ajax实现)
	@RequestMapping("/check_list2")
	@ResponseBody
	public pagebean getPage2(@RequestParam(defaultValue="1")int page,QuerySalary qs,HttpSession sess)
	{
		/*Emp e=(Emp)sess.getAttribute("loger");
		System.out.println(e.getUs_id()+","+e.getUs_name());*/
		pagebean pb=biz.getPage2(qs, page);
	    
		sess.setAttribute("pb", pb);
		return pb;
	}
		
	
	//财务：列表页面(ajax实现)
	@RequestMapping("/list")
	@ResponseBody
	public pagebean getPage(@RequestParam(defaultValue="1")int page,QuerySalary qs,HttpSession sess)
	{
		pagebean pb=biz.getPage(qs, page);
		sess.setAttribute("pb", pb);
		return pb;
	}
		
	
	//财务：待发放薪资页面(ajax实现)
	@RequestMapping("/list_ff")
	@ResponseBody
	public pagebean getPage_ff(@RequestParam(defaultValue="1")int page,QuerySalary qs,HttpSession sess)
	{
		pagebean pb=biz.getPage_ff(qs, page);
		sess.setAttribute("pb", pb);
		return pb;
	}

	//薪资发放
	@RequestMapping("/modstu_ff")
	public String modstatus_ff(long sa_id,HttpSession sess)
	{
		Salary ss=biz.getById(sa_id);
		ss.setSa_status("已发放");

		biz.update_sal(ss);
		return "salary_list";
	}
	
	//财务：状态为已删的页面(ajax实现)
	@RequestMapping("/list_del")
	@ResponseBody
	public pagebean getPage_del(@RequestParam(defaultValue="1")int page,QuerySalary qs,HttpSession sess)
	{
		pagebean pb=biz.getPage_del(qs, page);
		sess.setAttribute("pb", pb);
		return pb;
	}

	
	//取消是"已删除"的状态
	@RequestMapping("/get_delxx")
	public String getdelxx(long sa_id,HttpSession sess)
	{
		Salary ss=biz.getById(sa_id);
		ss.setSa_status("新增");

		biz.update_sal(ss);
		return "salary_list";
	}
	
	
	//将薪资信息导出到excel中
    @RequestMapping("/export")
    @ResponseBody
	public void export(HttpServletRequest request,HttpServletResponse response) throws Exception
	  {
	        response.reset(); //清除buffer缓存  
	        //Map<String,Object> map=new HashMap<String,Object>();  
	        // 指定下载的文件名  
	        response.setContentType("application/vnd.ms-excel;charset=UTF-8");  
	        response.setHeader("Content-Disposition","attachment;filename="+new String("员工薪资表.xlsx".getBytes(),"iso-8859-1"));
	        //导出Excel对象  
	        XSSFWorkbook workbook = biz.emexcel();
	        OutputStream output;  
	        try {  
	            output = response.getOutputStream();  
	            BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);  
	            bufferedOutput.flush();  
	            workbook.write(bufferedOutput);  
	            bufferedOutput.close();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }
	  }

	
	
	
	
	/*//财务：列表页面(普通实现实现)
	@RequestMapping("/list1")
	public String list1(HttpSession sess)
	{
		List<Salary> list=biz.getAll();
		sess.setAttribute("slist", list);
		return "salary_apply1";
	}*/
}
